package com.maduar.batch.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-07-08 23:03
 */
@Configuration
@EnableBatchProcessing
public class SingleStepJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private String helloWorldJob = "helloWorldJob";

    @Bean
    public Job helloWorldJob() {
        return jobBuilderFactory.get(helloWorldJob)
                .start(singleStep())
                .build();
    }

    @Bean
    public Step singleStep() {
        return stepBuilderFactory.get("singleStep")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("helloWorld");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }
}
